Shader "GridSystem/GridCellShader"
{
    Properties
    {
        _FillColor ("填充颜色", Color) = (1, 1, 1, 0.5)
        _EdgeColor ("边缘颜色", Color) = (0, 0, 0, 1)
        _LeftEdgeSize ("左边缘厚度", Range(0, 0.5)) = 0.1
        _RightEdgeSize ("右边缘厚度", Range(0, 0.5)) = 0.1
        _TopEdgeSize ("上边缘厚度", Range(0, 0.5)) = 0.1
        _BottomEdgeSize ("下边缘厚度", Range(0, 0.5)) = 0.1
    }
    SubShader
    {
        Tags { "Queue"="Transparent" "RenderType"="Transparent" }

        Pass
        {
            Cull Off 
            ZWrite Off 
            Blend SrcAlpha OneMinusSrcAlpha

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            fixed4 _FillColor;
            fixed4 _EdgeColor;
            half _LeftEdgeSize;
            half _RightEdgeSize;
            half _TopEdgeSize;
            half _BottomEdgeSize;

            struct appdata
            {
                float4 vertex : POSITION;
                half2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                half2 uv : TEXCOORD0;
            };
            
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                half2 uv = i.uv; 

                half disToTop = 1.0 - uv.y;
                half disToBottom = uv.y;
                half disToLeft = uv.x;
                half disToRight = 1.0 - uv.x;

                // 返回1说明当前像素点已经不在边框范围内，所以画填充色。反之返回0说明当前像素点还在边框范围内，所以画边框色
                half topEdgeFactor = step(_TopEdgeSize, disToTop); 
                half bottomEdgeFactor = step(_BottomEdgeSize, disToBottom);
                half leftEdgeFactor = step(_LeftEdgeSize, disToLeft);
                half rightEdgeFactor = step(_RightEdgeSize, disToRight);

                // 上面只要符合一个条件为0，就画边框色，所以取个最小值就行
                half edgeFactor = min(min(topEdgeFactor, bottomEdgeFactor), min(leftEdgeFactor, rightEdgeFactor));

                // 做个插值，因为edgeFactor只有2种情况，0和1。我们0画边框色，1画填充色
                fixed4 finalColor = lerp(_EdgeColor, _FillColor, edgeFactor);

                return finalColor;
            }
            ENDCG
        }
    }
}